package crypto.util;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
 * DOCUMENT ME!
 *
 * @author gato
 */
public class Util {
  /**
   * Metodo para transformar un numero decimal en un numero de base 36
   *
   * @param posic a transformar [Integer]
   * @param block DOCUMENT ME!
   *
   * @return Numero transformado [String]
   */
  public static String convertDecimalB36(int posic, int block) {
    String value  = Integer.toString(posic, 36);
    int    length = Util.estimateLengthConvertion(block);

    while (value.length() < length) {
      value += ((char) (RandomCrypto.getRandomInt(36) + 58));
    }

    return value;
  }

  /**
   * Metodo para transformar un numero en base 36 a un numero en base decimal
   *
   * @param num en Base 36 [String]
   *
   * @return Numero en decimal [Integer]
   */
  public static int convertB36Decimal(String num) {
    num = Util.prepareNumberConvertion(num);

    return Integer.parseInt(num, 36);
  }

  /**
   * DOCUMENT ME!
   *
   * @param block DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public static int estimateLengthConvertion(int block) {
    int    lengthBlock = block * 500;

    String value       = Integer.toString(lengthBlock, 36);

    return value.length();
  }

  private static String prepareNumberConvertion(String number) {
    char[] numSplit = number.toCharArray();

    for (int i = 0; i < numSplit.length; i++) {
      if ((numSplit[i] > 57) && (numSplit[i] < 95)) {
        numSplit[i] = ' ';
      }
    }

    return new String(numSplit).trim();
  }

  /**
   * DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  public static String selectFile() {
    JFrame       frame = new JFrame();
    JFileChooser fc    = new JFileChooser();
    File         file  = null;

    fc                 = new JFileChooser();
    fc.showOpenDialog(frame);
    file = fc.getSelectedFile();

    String path = "";

    try {
      path = file.getAbsolutePath();
    } catch (java.lang.NullPointerException ex) {
    }

    return path;
  }
}
